package com.example.tourpal.user;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.example.tourpal.MainActivity;
import com.example.tourpal.R;
import com.example.tourpal.model.User;
import com.example.tourpal.model.UserDataManager;
import com.example.tourpal.model.UserDataManager.RegisterLishenter;
import com.example.tourpal.util.SimpleCrypto;
import com.example.tourpal.util.Tools;

public class RegisterActivity extends Activity {
	private static final String TAG = "chengongguo/registerActivity";
	private Tools mTools;

	public static final int REQUEST_REGISTER = 102;
	public static final int RESULT_REGISTER = 103;
	private ImageView mBackTextView;
	private TextView mTitleTextView;
	private EditText mAccountEditText;
	private EditText mPasswordEditText;
	private EditText muserNameEditText;
	private Button mRegisterButton;
	private RadioGroup mRadioGroup;;
	private String mAccount;
	private String mPassword;
	private String muserName;
	private int mSex = 1;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_register_view);
		initCommonManager();
		initContentView();
	}

	private void initCommonManager() {
		// TODO Auto-generated method stub
		mTools = new Tools(this, TAG);
		// FrontiaManager.getInstance(this);
	}

	private void initContentView() {
		// TODO Auto-generated method stub
		mBackTextView = (ImageView) findViewById(R.id.back);
		mBackTextView.setOnClickListener(mListener);
		mTitleTextView = (TextView) findViewById(R.id.textview_user_title);
		mTitleTextView.setText(R.string.user_register);
		mAccountEditText = (EditText) findViewById(R.id.edittext_register_account);
		mPasswordEditText = (EditText) findViewById(R.id.edittext_register_password);
		muserNameEditText = (EditText) findViewById(R.id.edittext_register_userName);
		mRegisterButton = (Button) findViewById(R.id.button_register);
		mRegisterButton.setOnClickListener(mListener);
		mRadioGroup = (RadioGroup) findViewById(R.id.layout_login_radiobutton);
		mRadioGroup.setOnCheckedChangeListener(mOnCheckedChangeListener);
	}

	private OnClickListener mListener = new OnClickListener() {
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.button_register:
				register();
				// findDatefromLocation();
				break;
			case R.id.back:
				finish();
				break;
			}
		}
	};

	private OnCheckedChangeListener mOnCheckedChangeListener = new OnCheckedChangeListener() {
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			mSex = (checkedId == R.id.radiobutton_register_male) ? 1 : 0;
		}
	};

	private void register() {
		// TODO Auto-generated method stub
		if (isEditTextValid()) {
			final User user = new User(1, mAccount, muserName, mSex, mPassword);
			UserDataManager userDataManager = new UserDataManager(this);
			userDataManager.register(user, new RegisterLishenter() {
				@Override
				public void onRegister() {
					stratMainActivity(user);
				}
			});

		}
	}

	private void stratMainActivity(User user) {
		// TODO Auto-generated method stub
		Intent intent = new Intent(this, MainActivity.class);
		Bundle userBundle = new Bundle();
		userBundle.putSerializable("user", user);
		intent.putExtras(userBundle);
		setResult(RESULT_REGISTER, intent);
		finish();
	}

	public boolean isEditTextValid() {
		mAccount = mAccountEditText.getText().toString().trim();
		String password = mPasswordEditText.getText().toString().trim();
		try {
			mPassword = SimpleCrypto.encrypt(SimpleCrypto.SEED, password);
			mTools.showLog(mPassword);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		muserName = muserNameEditText.getText().toString().trim();
		if (mAccount.equals("")) {
			mTools.showToast(getString(R.string.user_account_empty));
			return false;
		} else if (mPassword.equals("")) {
			mTools.showToast(getString(R.string.user_pwd_empty));
			return false;
		} else if (muserName.equals("")) {
			mTools.showToast(getString(R.string.user_userName_empty));
			return false;
		}
		return true;
	}

	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
		overridePendingTransition(R.anim.main_enter, R.anim.sub_exit);
	}
}